package com.woniuxy.happylife.order.service.Impl;

import com.woniuxy.happylife.entity.MallOrder;
import com.woniuxy.happylife.entity.MallOrderItem;
import com.woniuxy.happylife.entity.Order;
import com.woniuxy.happylife.order.dao.OrderDao;
import com.woniuxy.happylife.order.service.OrderService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @Title: OrderServiceImpl
 * @ProjectName: happy-life
 * @Description: TODO
 * @Author: chowinggum
 * @Date: 2022/2/18 15:21
 */
@Service
public class OrderServiceImpl implements OrderService {
   @Resource
    OrderDao orderDao;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    RedissonClient redissonClient;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Override
    public void killGoods(int uid) {
        RLock lock = redissonClient.getLock("orderlock");
        lock.lock();
        //采用redis秒杀商品,剩余商品数
        ValueOperations valueOperations = redisTemplate.opsForValue();

        //每次取出一件商品
        String stock = valueOperations.get("stock").toString();


        if(Integer.parseInt(stock)>0){
            //随机来一个人，商品数减一
            valueOperations.decrement("stock");
            Order order = new Order();
            String orderno = UUID.randomUUID().toString().replace("-","").substring(0,15);
            Integer status= 0;
            order.setOrderno(orderno);
            order.setStatus(status);
            order.setUid(uid);
            rabbitTemplate.convertAndSend("orderexchange","order",order, new MessagePostProcessor() {
                @Override
                public Message postProcessMessage(Message message) throws AmqpException {
                    message.getMessageProperties().setExpiration("30000");
                    return message;
                }
            });

            System.out.println("下单成功，订单号"+order);
            redisTemplate.opsForValue().set(uid+"",0);
        }else{
            System.out.println("抢完了");
        }
        lock.unlock();
    }

    @Override
    public int add(Order order) {
        return orderDao.add(order);

    }

    @Override
    public MallOrder findByOrderno(String orderno) {
        return orderDao.findByOrderno(orderno);
    }

    @Override
    public int countByUid(int uid) {
        return orderDao.countByUid(uid);
    }

    @Override
    public List<MallOrder> findByUid(int uid) {
        return orderDao.findByUid(uid);
    }

    @Override
    public List<MallOrder> findpartByUid(int uid) {
        return orderDao.findpartByUid(uid);
    }

    @Override
    public int addItem(MallOrderItem orderitem) {
        return orderDao.addItem(orderitem);
    }

    @Override
    public List<MallOrderItem> findItemByOrderId(int orderId) {
        return orderDao.findItemByOrderId(orderId);
    }

    @Override
    public int delOrder(String orderno) {
        return orderDao.delOrder(orderno);
    }
}
